package com.ytjj.qmyx.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.eunm.BannerTypeEnums;
import com.ytjj.qmyx.admin.eunm.DynamicCategoryEnums;
import com.ytjj.qmyx.admin.eunm.DynamicNameCode;
import com.ytjj.qmyx.admin.model.request.*;
import com.ytjj.qmyx.admin.model.response.BannerTypeResponse;
import com.ytjj.qmyx.admin.model.response.DynamicCategoryResp;
import com.ytjj.qmyx.admin.model.response.DynamicCategoryResponse;
import com.ytjj.qmyx.admin.service.DynamicService;
import com.ytjj.qmyx.admin.service.SpecialService;
import com.ytjj.qmyx.mall.mapper.DynamicCommentMapper;
import com.ytjj.qmyx.mall.model.DynamicComment;
import com.ytjj.qmyx.mall.model.DynamicCommentExample;
import com.ytjj.qmyx.mall.model.DynamicCommentReply;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.ytjj.qmyx.admin.eunm.DynamicExposureTypeEnums.getNameByCode;


/**
 * @Description 动态管理
 *
 * @Version 1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/dynamic")
public class DynamicController {


    @Resource
    private DynamicService dynamicService;

    @Resource
    private SpecialService specialService;
    @Autowired
    private RedisService redisService;

    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    @GetMapping("/types")
    public CommonResult queryTypes() {
        String bannerStr = redisService.get("admin:banner_type");
        if (StringUtils.isEmpty(bannerStr)) {
            BannerTypeEnums[] enums = BannerTypeEnums.values();
            List<BannerTypeResponse> array = new ArrayList<>();
            Arrays.stream(enums).forEach(item -> {
                BannerTypeResponse response = new BannerTypeResponse();
                response.setLabel(item.getActivityCode());
                response.setValue(item.getActivityType());
                array.add(response);
            });
            return CommonResult.success(array);
        }
        List<BannerTypeResponse> array = JSONArray.parseArray(bannerStr, BannerTypeResponse.class);
        return CommonResult.success(array);
    }

    /**
     * 新增广告图
     *
     * @return
     */
    @PostMapping("/addAdImg")
    @OperLog(operModule = "广告模块", operDesc = "新增广告图", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addAdImg(@RequestBody AdRequest adRequest) {
        dynamicService.addAdImg(adRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 删除广告图
     *
     * @param adRequest
     * @return
     */
    @PostMapping("/isShowAdImg")
    @OperLog(operModule = "广告模块", operDesc = "删除广告图", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult isShowAdImg(@RequestBody AdRequest adRequest) {
        dynamicService.isShowAdImg(adRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 获取广告图列表
     *
     * @return
     */
    @PostMapping("/getAdImgs")
    public CommonResult getAdImgs(@RequestBody PageRequest pageRequest) {
        return CommonResult.success(dynamicService.getAdImgs(pageRequest));
    }

    /**
     * 根据id定位广告图
     *
     * @param id
     * @return
     */
    @GetMapping("/positionImg")
    public CommonResult positionImg(@RequestParam("id") Integer id) {
        return CommonResult.success(dynamicService.positionImg(id));
    }

    /**
     * 更新广告图信息
     *
     * @param adRequest
     * @return
     */
    @PostMapping("/editAdImg")
    @OperLog(operModule = "广告模块", operDesc = "更新广告图信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult editAdImg(@RequestBody AdRequest adRequest) {
        dynamicService.updateAdImg(adRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 根据广告图类型搜索
     *
     * @param adRequest
     * @return
     */
    @PostMapping("/getAdImgList")
    public CommonResult searchByType(@RequestBody AdRequest adRequest) {
        return CommonResult.success(dynamicService.searchByType(adRequest));
    }

    /**
     * 获取广告类型枚举列表
     * @return
     */
    @PostMapping("/getDynamicCode")
    public CommonResult getDynamicCode(){
        return CommonResult.success(DynamicNameCode.values());
    }

    /**
     * 增加动态管理
     *
     * @param dynamicRequest
     * @return
     */
    @PostMapping("/addDynamic")
    @OperLog(operModule = "动态模块", operDesc = "增加动态", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addDynamic(@RequestBody DynamicRequest dynamicRequest) {
        dynamicService.addDynamic(dynamicRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 视频动态 通过/不通过
     *
     * @param dynamicRequest
     * @return
     */
    @PostMapping("/videoPass")
    @OperLog(operModule = "动态模块", operDesc = "视频审核", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult videoPass(@RequestBody DynamicRequest dynamicRequest) {
        dynamicService.videoPass(dynamicRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    @GetMapping("/failReason")
    public CommonResult failReason() {
        String failReasonStr = "";
        try {
            failReasonStr = redisService.get("dynamic:fail:reason");
        } catch (Exception e){}
        if (StringUtils.isEmpty(failReasonStr)) {
            String failReason = "标题违规,标题字数不足,图片内容违规,图片不清晰,图片数量不足,视频时长不足,视频不清晰,视频内容违规,测评字数不足,测评内容违规,测评内容不相符,其它";
            redisService.set("dynamic:fail:reason", failReason);
            failReasonStr = failReason;
        }
        String[] failReasons = failReasonStr.split(",");
        List<String> results = Arrays.stream(failReasons).collect(Collectors.toList());
        return CommonResult.success(results);
    }

    /**
     * 推荐
     *
     * @param dynamicRequest
     * @return
     */
    @PostMapping("/dynamicRecommend")
    @OperLog(operModule = "动态模块", operDesc = "推荐修改", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult dynamicRecommend(@RequestBody DynamicRequest dynamicRequest) {
        dynamicService.dynamicRecommend(dynamicRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 定位该动态
     *
     * @return
     */
    @GetMapping("/positionDynamic")
    public CommonResult positionDynamic(@RequestParam("id") Integer id) {
        return CommonResult.success(dynamicService.positionDynamic(id));
    }


    /**
     * 更新动态信息
     *
     * @param dynamicRequest
     * @return
     */
    //@RequiresPermissions("dynamic:edit")
    @PostMapping("/editDynamic")
    @OperLog(operModule = "动态模块", operDesc = "更新动态信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult editDynamic(@RequestBody DynamicRequest dynamicRequest) {
        dynamicService.editDynamic(dynamicRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 获取动态列表
     *
     * @param dynamicRequest
     * @return
     */
    @PostMapping("/getDynamicList")
    public CommonResult searchDynamic(@RequestBody DynamicRequest dynamicRequest) {
        return dynamicService.searchDynamic(dynamicRequest);
    }

    @GetMapping("/getDynamicSpecialList")
    public CommonResult getDynamicSpecialList() {
        return CommonResult.success(specialService.getDynamicSpecialList());
    }

    /**
     * 删除动态
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteDynamic")
    @OperLog(operModule = "动态模块", operDesc = "删除动态", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteDynamic(@RequestParam("id") Integer id) {
        dynamicService.deleteDynamic(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
     * 新增图床图片
     *
     * @param bedImgRequest
     * @return
     */
    @PostMapping("/addBedImg")
    @OperLog(operModule = "图床模块", operDesc = "新增图床图片", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addBedImg(@RequestBody BedImgRequest bedImgRequest) {
        dynamicService.addBedImg(bedImgRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 获取图床信息
     *
     * @param id
     * @return
     */
    @GetMapping("/acquireBedImg")
    public CommonResult acquireBedImg(@RequestParam("id") Integer id) {
        return CommonResult.success(dynamicService.acquireBedImg(id));
    }

    /**
     * 更新图床图片信息
     *
     * @param bedImgRequest
     * @return
     */
    @PostMapping("/editBedImg")
    @OperLog(operModule = "图床模块", operDesc = "更新图床图片信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult editBedImg(@RequestBody BedImgRequest bedImgRequest) {
        dynamicService.editBedImg(bedImgRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 获取图床信息列表
     *
     * @param bedImgRequest
     * @return
     */
    @PostMapping("/getBedImgList")
    public CommonResult getBedImgList(@RequestBody BedImgRequest bedImgRequest) {
        return CommonResult.success(dynamicService.getBedImgList(bedImgRequest));
    }


    /**
     * 获取动态专区列表
     *
     * @return
     */
    @GetMapping("/querySpecialList")
    public CommonResult querySpecialList() {
        return CommonResult.success(dynamicService.querySpecialList());
    }


    /**
     * 获取动态评论列表(只查1级评论)
     *
     * @return
     */
    @PostMapping("/getDynamicCommentList")
    public CommonResult getDynamicCommentList(@RequestBody DynamicCommentRequest dynamicCommentRequest) {
        return CommonResult.success(dynamicService.getDynamicCommentList(dynamicCommentRequest));
    }

    /**
     * 根据1级评论查询下面的子集评论
     *
     * @return
     */
    @GetMapping("/getChildrenByPid")
    public CommonResult getChildrenByPid(@RequestParam Integer pid) {
        return CommonResult.success(dynamicService.getChildrenByPid(pid));
    }

    /**
     * 添加评论
     *
     * @return
     */
    @PostMapping("/addDynamicComment")
    public CommonResult addDynamicComment(@RequestBody DynamicComment dynamicComment) {
        int count = dynamicService.addDynamicComment(dynamicComment);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 编辑评论
     *
     * @return
     */
    @PostMapping("/updateDynamicComment")
    public CommonResult updateDynamicComment(@RequestBody DynamicComment dynamicComment) {
        int count = dynamicService.updateDynamicComment(dynamicComment);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 评论审核
     *
     * @param dynamicComment
     * @return
     */
    @PostMapping("/updateDynamicCommentStatus")
    public CommonResult updateDynamicCommentStatus(@RequestBody DynamicComment dynamicComment){
        int count = dynamicService.updateDynamicCommentStatus(dynamicComment.getId(),dynamicComment.getStatus(),dynamicComment.getRemark());
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
    /**
     * 删除评价
     *
     * @return
     */
    @GetMapping("/deleteDynamicComment")
    public CommonResult deleteDynamicComment(@RequestParam Integer id) {
        int count = dynamicService.deleteDynamicComment(id);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取动态测评的分类
     */
    @GetMapping("/getDynamicCategory")
    public CommonResult getDynamicCategory(){
        List<DynamicCategoryResp> list = new ArrayList<>();
        DynamicCategoryEnums[] typeList = DynamicCategoryEnums.values();
        for (DynamicCategoryEnums type : typeList) {
            DynamicCategoryResp atp = new DynamicCategoryResp();
            atp.setCode(type.getCode());
            atp.setName(DynamicCategoryEnums.getNameByCode(type.getCode()));
            list.add(atp);
        }
        return CommonResult.success(list);
    }


    /**
     * 添加或编辑评论
     * v4.2.2
     * @return
     */
    @PostMapping("/addOrEditComment")
    public CommonResult addOrEditComment(@RequestBody DynamicComment dynamicComment) {
        int count = dynamicService.addOrEditComment(dynamicComment);
        return count>0 ? CommonResult.success(count) : CommonResult.failed();
    }

    /**
     * 添加或者编辑回复
     * v4.2.2
     * @return
     */
    @PostMapping("/addOrEditReply")
    public CommonResult addOrEditReply(@RequestBody DynamicCommentReplyRequest dynamicCommentReplyRequest) {
        int count = dynamicService.addOrEditReply(dynamicCommentReplyRequest);
        return count>0 ? CommonResult.success(count) : CommonResult.failed();
    }

    /**
    * 删除评论或者删除回复*
    * v4.2.2
    * @param id:
    * @param type: 0-评论 1-回复
    * @returns: com.ytjj.common.api.CommonResult
    *
    */
    @GetMapping("/delCommentOrReply")
    public CommonResult delCommentOrReply(Integer id,Integer type,String reason) {
        int count = dynamicService.delCommentOrReply(id,type,reason);
        return count>0 ? CommonResult.success(count) : CommonResult.failed();
    }

    /**
     * 隐藏或者显示评论；隐藏或者显示回复
     * v4.2.2
     * @param id:
     * @param type: 0-评论 1-回复
     * @returns: com.ytjj.common.api.CommonResult
     *
     */
    @GetMapping("/showOrHide")
    public CommonResult showOrHide(Integer id,Integer type,Integer isShow) {
        int count = dynamicService.showOrHide(id,type,isShow);
        return count>0 ? CommonResult.success(count) : CommonResult.failed();
    }

    /**
     * 获取动态评论列表(一级评论)
     * v4.2.2
     * @return
     */
    @PostMapping("/getDynamicComments")
    public CommonResult getDynamicComments(@RequestBody DynamicCommentRequest dynamicCommentRequest) {
        return CommonResult.success(dynamicService.getComments(dynamicCommentRequest));
    }

    /**
     * 根据评论获取评论的回复
     * v4.2.2
     * @return
     */
    @PostMapping("/getReplyByComment")
    public CommonResult getReplyByComment(@RequestBody DynamicCommentReplyRequest dynamicCommentReplyRequest) {
        return CommonResult.success(dynamicService.getReplyByComment(dynamicCommentReplyRequest));
    }



}
